package com.example.admin.goodnews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.DuanZiDataEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/10/14.
 */

public class DuanZiAdapter extends RecyclerView.Adapter<DuanZiAdapter.ViewHolder> {
    private Context context;
    private List<DuanZiDataEntity.ShowapiResBodyBean.ContentlistBean> datalists = new ArrayList<>();// 适配器需要的数据列表

    public DuanZiAdapter(Context context, List<DuanZiDataEntity.ShowapiResBodyBean.ContentlistBean> datalists) {
        this.context = context;
        this.datalists = datalists;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_duanzi, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String text = datalists.get(position).getText();
        // 去除文本中的空格换行符等内容
        text.replaceAll("\\s", "");
        text.replaceAll("\n", "");
        text.replaceAll("\r", "");
        text.replaceAll("\t", "");
        text.replaceAll("\t1", "");
        text.replaceAll("\t2", "");
        text.replaceAll("\t3", "");
        text.replaceAll("\4", "");
        text.replaceAll("\t5", "");
        holder.tv_duanzi_title.setText(datalists.get(position).getTitle());
        // holder.tv_duanzi_content.setText(text);
        holder.tv_duanzi_content.setText(Html.fromHtml(text).toString());// 去除文本中的HTML <p>标签
        holder.tv_duanzi_time.setText(datalists.get(position).getCt().substring(0, 19));

    }


    @Override
    public int getItemCount() {
        return datalists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_duanzi_title;// 段子标题
        private TextView tv_duanzi_content;// 段子正文
        private TextView tv_duanzi_time;// 段子发布时间

        public ViewHolder(View itemView) {
            super(itemView);
            tv_duanzi_title = (TextView) itemView.findViewById(R.id.tv_duanzi_title);// 段子标题
            tv_duanzi_content = (TextView) itemView.findViewById(R.id.tv_duanzi_content);// 段子正文
            tv_duanzi_time = (TextView) itemView.findViewById(R.id.tv_duanzi_time);// 段子发布时间
        }
    }

}
